#!/bin/bash

ospkg_source_china()
{
	# https://mirrors.ustc.edu.cn/help/fedora.html
	test -f /etc/yum.repos.d/fedora.repo &&
	$SUDO sed -e 's|^metalink=|#metalink=|g' \
         -e 's|^#baseurl=http://download.example/pub/fedora/linux|baseurl=https://mirrors.ustc.edu.cn/fedora|g' \
         -i.bak \
         /etc/yum.repos.d/fedora.repo \
         /etc/yum.repos.d/fedora-modular.repo \
         /etc/yum.repos.d/fedora-updates.repo \
         /etc/yum.repos.d/fedora-updates-modular.repo

	# https://mirrors.ustc.edu.cn/help/centos.html
	# CentOS 8
	test -f /etc/yum.repos.d/CentOS-Linux-BaseOS.repo &&
	$SUDO sed -e 's|^mirrorlist=|#mirrorlist=|g' \
         -e 's|^#baseurl=http://mirror.centos.org/$contentdir/$releasever/|baseurl=https://mirrors.ustc.edu.cn/centos/8-stream/|g' \
         -i.bak \
         /etc/yum.repos.d/CentOS-Linux-AppStream.repo \
         /etc/yum.repos.d/CentOS-Linux-BaseOS.repo \
         /etc/yum.repos.d/CentOS-Linux-Extras.repo \
         /etc/yum.repos.d/CentOS-Linux-PowerTools.repo

	# CentOS 7
	test -f /etc/yum.repos.d/CentOS-Base.repo &&
	$SUDO sed -e 's|^mirrorlist=|#mirrorlist=|g' \
         -e 's|^#baseurl=http://mirror.centos.org/centos|baseurl=https://mirrors.ustc.edu.cn/centos|g' \
         -i.bak \
         /etc/yum.repos.d/CentOS-Base.repo
}

ospkg_update()
{
	ospkg_source_china
	support_repo_epel
}

ospkg_install()
{
	local pkg nopkgs err
	for pkg
	do
		nopkgs=$(rpm --query $pkg | awk '{print $2}')
		[[ -n $nopkgs ]] || continue
		$SUDO yum install -y -q $nopkgs || err=$?
	done

	return $err
}

ospkg_list()
{
	opt_all=
	has_cmd dnf && opt_all=--all

	yum list $opt_all -q | cut -f1 -d. |
		grep -v -E '\-(doc|help|debuginfo|debugsource)$' | sort -u
}

support_repo_epel()
{
	if [ "${_system_name_lowercase}" = "centos" ] &&
		! rpm -q epel-release >/dev/null; then
		yum install -y epel-release >/dev/null 2>&1 &&
		# https://mirrors.ustc.edu.cn/help/epel.html
		$SUDO sed -e 's|^metalink=|#metalink=|g' \
			 -e 's|^#baseurl=https\?://download.fedoraproject.org/pub/epel/|baseurl=https://mirrors.ustc.edu.cn/epel/|g' \
			 -e 's|^#baseurl=https\?://download.example/pub/epel/|baseurl=https://mirrors.ustc.edu.cn/epel/|g' \
			 -i.bak \
			 /etc/yum.repos.d/epel.repo
	fi

	if has_cmd dnf; then
		dnf -q makecache --timer
	else
		yum -q makecache
	fi
}
